<?php

namespace App\Service\OpenShop\SiteLocation;

use App\Models\OpenShop\Common\MallSettleInShopInfoModel;
use App\Models\OpenShop\Common\SiteLocationMallModel;
use App\Service\BaseService;

class MallService extends BaseService
{

    /**
     * 根据商场ID获得商场每层的数据
     * @param string $mallId
     * @return array
     */
    public static function floorGuide(string $mallId): array
    {
        if (!strlen($mallId)) {
            return [];
        }
        $mallQuery = MallSettleInShopInfoModel::query()
            ->where('mallId', $mallId)
            ->with(['siteLocationMallRel'])
            ->get();

        if ($mallQuery === null) {
            return [];
        }

        $allStore = [
            "tagName"     => "全部",
            "classifyArr" => [],
            "children"    => []
        ];
        $mallQuery->each(function (MallSettleInShopInfoModel $mallSettleInShopInfoModel) use (&$eachFloorData, &$allStore, &$recommendFloor) {
            //获得推荐楼层
            $recommendFloor = $mallSettleInShopInfoModel->siteLocationMallRel ? $mallSettleInShopInfoModel->siteLocationMallRel->recommendFloor : '';
            //临时数据
            $tempData               = [
                'storeName'        => $mallSettleInShopInfoModel->storeName,
                'floor'            => $mallSettleInShopInfoModel->floor ?: '-',
                'classify'         => $mallSettleInShopInfoModel->firstType ?: '-',
                'classifyChildren' => $mallSettleInShopInfoModel->secondType ?: '-',
                'star'             => $mallSettleInShopInfoModel->star,
                'price'            => $mallSettleInShopInfoModel->price,
            ];
            $allStore['children'][] = $tempData;
            //楼层数据
            if (mb_strlen($mallSettleInShopInfoModel->floor)) {
                $eachFloorData[$mallSettleInShopInfoModel->floor][] = $tempData;
            }
        });
        //全部的分类数据
        $allStore['classifyArr'] = collect($allStore['children'])->pluck('classify')->unique()->values()->toArray();
        return array_merge([$allStore], collect($eachFloorData)->map(function ($floorData, $floor) use ($recommendFloor) {
            return [
                'tagName'     => $floor,
                'classifyArr' => array_merge(['全部'], collect($floorData)->pluck('classify')->unique()->values()->toArray()),
                'children'    => $floorData,
                'isRecommend' => $floor == $recommendFloor,
            ];
        })->values()->toArray());
    }

    /**
     * 根据商场ID和楼层获得该楼层的所有店铺
     * @param string $mallId
     * @param string $floor
     * @return array
     */
    public static function getStoreByMallIdAndFloor(string $mallId, string $floor): array
    {
        return MallSettleInShopInfoModel::query()
            ->where('mallId', $mallId)
            ->where('floor', $floor)
            ->get()
            ->map(function (MallSettleInShopInfoModel $mallSettleInShopInfoModel) {
                return [
                    'storeName'        => $mallSettleInShopInfoModel->storeName,
                    'floor'            => $mallSettleInShopInfoModel->floor ?: '-',
                    'classify'         => $mallSettleInShopInfoModel->firstType ?: '-',
                    'classifyChildren' => $mallSettleInShopInfoModel->secondType ?: '-',
                    'star'             => $mallSettleInShopInfoModel->star,
                    'price'            => $mallSettleInShopInfoModel->price,
                ];
            })->toArray();
    }

    /**
     * 通过商场ID获得推荐楼层店铺
     * @param string $mallId
     * @return array
     */
    public static function getRecommendFloorStoreByMallId(string $mallId): array
    {
        //推荐楼层
        $recommendFloor = (string)SiteLocationMallModel::query()->where('mallId', $mallId)->value('recommendFloor');
        if (!mb_strlen($recommendFloor)) {
            return [];
        }
        return self::getStoreByMallIdAndFloor($mallId, $recommendFloor);
    }
}
